package com.ftg.learn.service.imple;

import com.ftg.learn.domain.DeptDomain;
import com.ftg.learn.mapper.DeptMapper;
import com.ftg.learn.service.DeptService;
import com.ftg.learn.util.PageUtil;
import com.ftg.learn.util.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Slf4j 日志打印框架
 * @author 布衣
 */
@Service
@Slf4j
public class DeptServiceImpl implements DeptService {

    @Resource
    DeptMapper deptMapper;

    /**
     * 分页功能
     *
     * @param dname    模糊查询的关键字
     * @param pageNum  页码
     * @param pageSize 显示 条数
     * @return 封装类
     */
    @Override
    public PageUtil<PageInfo<DeptDomain>> list(String dname, Integer pageNum, Integer pageSize) {
        log.info("分页开始");
        PageHelper.startPage(pageNum, pageSize);
        return PageUtil.success(R.QUERY_SUCCESS, new PageInfo<>(deptMapper.showAll(dname)));
    }

    public PageUtil<Integer> save(DeptDomain deptDomain) {
        log.info("添加开始");
        try {
            return PageUtil.success(R.INSERT_SUCCESS, deptMapper.save(deptDomain));
        } catch (Exception e) {
            log.error(e.getMessage());
            return PageUtil.fail(R.INSERT_FAIL);
        }
    }

    public PageUtil<Integer> modify(Integer deptno, DeptDomain deptDomain) {
        try {
            return PageUtil.success(R.INSERT_SUCCESS, deptMapper.modify(deptno, deptDomain));
        } catch (Exception e) {
            log.error(e.getMessage());
            return PageUtil.fail(R.UPDATE_FAIL);
        }
    }

    public PageUtil<Integer> deleteById(Integer id) {
        try {
            return PageUtil.success(R.INSERT_SUCCESS, deptMapper.deleteById(id));
        } catch (Exception e) {
            log.error(e.getMessage());
            return PageUtil.fail(R.DELETE_FAIL);
        }
    }
}
